package com.xci.platform.auth.controller;

import com.xci.platform.auth.entity.MessageEntity;
import com.xci.platform.auth.service.MessageService;
import com.xci.platform.annotation.Authorize;
import com.xci.platform.core.BoolMessage;
import com.xci.platform.core.GMap;
import com.xci.platform.core.JsonGrid;
import com.xci.platform.helper.StringHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * 系统消息控制器
 */
@Authorize
@Slf4j
@Controller
@RequestMapping("/auth/message")
public class MessageController extends AuthBaseController {
    /**
     * 系统消息服务
     */
    @Resource
    protected MessageService messageService;

    //region 页面视图

    /**
     * 系统消息首页
     */
    @GetMapping
    @Authorize(code = "SystemMessage")
    public String index(String receiveUserId, ModelMap map) {
        map.put("receiveUserId", receiveUserId);
        return "auth/message/index";
    }

    /**
     * 系统消息新建页面
     */
    @GetMapping("/create")
    @Authorize(code = "SystemMessage.Create")
    public String create(ModelMap map) {
        createMark(map);
        MessageEntity entity = new MessageEntity();
        entity.setId(StringHelper.guid());
        map.put("entity", entity);
        return "auth/message/edit";
    }

    /**
     * 系统消息详细页面
     */
    @GetMapping("/details")
    public String details(String id, String receiveUserId, ModelMap map) {
        MessageEntity entity = messageService.queryById(id);
        if (entity == null) return notFound();
        if (entity.getReadStatus() == 0 && StringHelper.isNotBlank(receiveUserId)) {//未读消息
            messageService.updateReadStatus(id);
            entity.setReadStatus(1);
            entity.setReadDatetime(new Date());
        }
        map.put("entity", entity);
        return "auth/message/details";
    }

    //endregion

    //region 数据处理

    @ResponseBody
    @PostMapping("/uncount")
    public BoolMessage uncount() {
        String userId = currentUser().getUserId();
        Integer count = messageService.queryUnReadCountByUserId(userId);
        int countValue = 0;
        if (count != null) {
            countValue = count;
        }
        return BoolMessage.success(GMap.newMap("count", countValue));
    }

    /**
     * 系统消息表格数据
     */
    @ResponseBody
    @GetMapping("/grid")
    @Authorize(code = "SystemMessage.Search")
    public JsonGrid grid(@RequestParam Map params) {
        return BoolMessage.grid(messageService.queryPage(params));
    }

    /**
     * 保存系统消息
     */
    @ResponseBody
    @PostMapping("/save")
    @Authorize(code = "SystemMessage.Create")
    public BoolMessage save(@ModelAttribute MessageEntity entity) {
        return messageService.send(entity.getTitle(), entity.getMessageContent(),
                currentUser().getUserId(), entity.getReceiveUserId());
    }

    /**
     * 删除系统消息
     */
    @ResponseBody
    @PostMapping("/delete")
    @Authorize(code = "SystemMessage.Delete")
    public BoolMessage delete(String ids) {
        for (String id : StringHelper.splitToArray(ids)) {
            messageService.delete(id);
        }
        return BoolMessage.True;
    }
    //endregion
}
